package com.oriental.manage.controller.specially;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.riskfront.api.enums.DataDictCode;
import com.oriental.riskfront.api.model.specially.DataDicDto;
import com.oriental.riskfront.api.service.DataDictInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yutao
 * Date: 2017/5/18
 * Time: 9:13
 * Desc：
 */
@Controller
@Slf4j
@RequestMapping("specially/dataDic/cache")
public class DataDicController {

    @Autowired
    private DataDictInterface dataDictInterface;

    private static   Map<String,Object > catchMap = new HashMap<>();

    @RequestMapping("init")
    public String init(){
        return "specially/searchDataDic";
    }

    @RequestMapping("get")
    @ResponseBody
    public ResponseDTO getDataDic(@RequestBody String[] keys){
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String, String> map = new HashMap<String, String>();
        Map<String,Object > resultMap = new HashMap<>();
        try{
            for (String key : keys) {
                if (catchMap.containsKey(key)) {
                    resultMap.put(key,catchMap.get(key));
                } else if(StringUtils.equals(key, DataDictCode.ADDRESS_Select.getKey())) {
                    List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress(key);
                    resultMap.put(key, dataAddress);
                    catchMap.put(key, dataAddress);
                }else if("ALL_AREA_CODE".equals(key)){
                    List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress("ADDRESS_Select");
                    Map<String,String> areaMap = new HashMap<>();
                    for (Map<String,Object> m:dataAddress
                            ) {
                        areaMap.put((String)m.get("areaCode"),(String)m.get("areaName"));
                    }
                    resultMap.put("ALL_AREA_CODE", areaMap);
                    catchMap.put("ALL_AREA_CODE", areaMap);
                }else{
                    map = dataDictInterface.getData(key);
                    catchMap.put(key, map);
                    resultMap.put(key,map);
                }

            }

            responseDTO.setSuccess(true);
        }catch(Exception e){
            log.error("获取特约商户数据字典失败", e);
            map.put("", "未找到相关数据");
            responseDTO.setSuccess(false);
        }finally {
            responseDTO.setObject(resultMap);
        }
        return responseDTO;
    }

    @RequestMapping("search")
    @ResponseBody
    public ResponseDTO<Pagination<DataDicDto>> queryDetail(@RequestBody Pagination<DataDicDto> pagination){
        ResponseDTO<Pagination<DataDicDto>> responseDTO = new ResponseDTO<>();
        try{
            log.info("查询特约商户数据字典参数:{}",pagination);
            List<DataDicDto> dataDicDtos = dataDictInterface.queryPage(pagination.getQueryBean());
            if(dataDicDtos !=null && dataDicDtos.size()>0){
                pagination.setRowCount(dataDicDtos.get(0).getRowCount());
            }
            pagination.setList(dataDicDtos);
            responseDTO.setObject(pagination);
            responseDTO.setSuccess(true);

        }catch (Exception e){
            log.error("查询特约商户数据字典失败:{}", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询异常");
        }
        return responseDTO;
    }


    @RequiresPermissions("specially_data_refresh")
    @RequestMapping("refresh")
    @ResponseBody
    public ResponseDTO<String> refresh(){
        ResponseDTO<String> responseDto = new ResponseDTO<>();
        try{
            log.info("******重新获取协会数据字典******");
            boolean result = dataDictInterface.refresh();
            if(result){
                catchMap.clear();
                responseDto.setSuccess(true);
            }else {
                responseDto.setSuccess(false);
            }

        }catch (Exception e) {
            log.error("特约商户重新获取数据字典异常:{}",e);
            responseDto.setSuccess(false);
        }
        return responseDto;
    }
}
